

import { MysqlDataSource } from "../../db/orm";
import SymbolOrderConfigService from "../symbol-order-config/SymbolOrderConfig.service";
import StrategyEntity from "./strategy.entity";
import WatchEntityService from '../watch/watch.service'
import { format } from 'prettier';
import { outLogger } from "ROOT/common/logger";

class StrategyService {
  repository = MysqlDataSource.getRepository(StrategyEntity);
  /**
   * 查询
   * @param {object} query
   */
  find = async function (query: { symbol?: string, userid?: number }) {
    const res = await this.repository.find({ where: { ...query, is_deleted: 0 } });
    return res;
  };

  /**
   * 查询单个
   * @param {object} query
   */
  findOne = async function (query: { id: number, userid?: number } | { symbol: string, name?: string }) {
    const target = await this.repository.findOne({ where: { ...query, is_deleted: 0 } });
    return target;
  };

  /**
   * 创建
   * @param {object} query
   */
  create = async function (data: Omit<StrategyEntity, 'id' | 'ctime' | 'utime'>) {
    const target = await this.findOne({ userid: data.userid, symbol: data.symbol, name: data.name });

    if (target) {
      target.is_deleted = 0;
      this.repository.save(target);
      return target
    }

    data.strategy = format(data.strategy, { parser: 'typescript' });
    const result = await this.repository.save(data);
    return result;
  };

  /**
   * 更新
   * @param {object} query
   */
  update = async function (query: { id: number, userid: number }, data: Partial<Omit<StrategyEntity, 'id'>>) {
    const target = await this.findOne({ id: query.id, userid: query.userid });
    if (target) {
      data.strategy = format(data.strategy, { parser: 'typescript' });
      Object.assign(target, data);
      return await this.repository.save(target);
    }
    throw Error(`${JSON.stringify(query)} 不存在 或者没有权限`)
  };
  /**
   * admin更新
   * @param {object} query
   */
  updateByAdmin = async function (query: { id: number }, data: Partial<Omit<StrategyEntity, 'id'>>) {
    const target = await this.findOne({ id: query.id });
    if (target) {
      data.strategy = format(data.strategy, { parser: 'typescript' });
      Object.assign(target, data);
      return await this.repository.save(target);
    }
    throw Error(`${JSON.stringify(query)} 不存在`)
  };
  /**
   * 删除1个
   * @param {object} query
   * @param { Document }
   */
  deleteOne = async function (query: Partial<StrategyEntity>) {
    const target = await this.findOne({ id: query.id, ...query });
    if (target) {
      await SymbolOrderConfigService.unbindStrategy(target.id);
      await WatchEntityService.unbindStrategy(target.id);
      return await this.repository.remove(target);
    }
    outLogger.error(`${JSON.stringify(query)} 删除失败`);
    return;
  };
  /**
   * 删除多个
   * @param {object} query
   * @param { Document }
   */
  deleteMany = async function (symbol: string, userid: number) {
    const list = await this.find({ symbol: symbol, userid });
    for (let i = 0; i < list.length; i++) {
      const element = list[i];
      await this.repository.remove(element);
    }
  };

}

export default new StrategyService();